package Demo01.Student;

import java.util.ArrayList;
import java.util.List;

/*
1.	创建Student类，属性：学号 姓名 年龄通过上面的字符串获取学号，姓名，年龄创建Student类，并将Student类的对象保存到一个ArrayList集合中
以下完成每个步骤完成需要定义方法：
1.写出3种方式遍历集合中的数据（for;for each;iterator）
2.查询里面有没有叫田七的学生如果存在则打印学生的具体信息（封装成方法，键盘输入姓名）
3.计算所有学生的平均年龄，最大年龄，最小年龄
4.如果存在名字叫张三的将名字改为张兵
5.删除年龄小于20岁 的学生从集合中

 */
public class Student {
    private int NIO;
    private String name;
    private int age;

    public Student(int NIO, String name, int age) {
        super();
        this.NIO = NIO;
        this.name = name;
        this.age = age;
    }

    public Student() {
        super();
    }

    public int getNIO() {
        return NIO;
    }

    public void setNIO(int NIO) {
        this.NIO = NIO;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Demo01.Student{" +
                "NIO=" + NIO +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


     static class Function {

         static Student find(List list, String str) {
            for (int i = 1; i < list.size(); i++) {
                if (str.equals(((Student) list.get(i)).getName())) {
                    return (Student) list.get(i);
                }
            }
            return null;
        }
    }



    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student s1 = new Student(1,"马可波罗",65);
        Student s2 = new Student(2,"戈雅",14);
        Student s3 = new Student(3,"澜",24);
        Student s4 = new Student(4,"镜",65);
        Student s5 = new Student(5,"桑启",83);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
//        Iterator<Demo01.Student> iterator = list.iterator();
//        while (iterator.hasNext()){
//            Demo01.Student next = iterator.next();
//            System.out.println(next.getNIO()+"  "+next.getName()+"  "+next.getAge());
//        }
//        System.out.println("**************************");
//
//        for (int i = 0; i < list.size(); i++) {
//            Demo01.Student s = list.get(i);
//            System.out.println(s.getNIO()+" "+s.getName()+"  "+s.getAge());
//            System.out.println("****************************");
//            System.out.println(list);
//        }
        for (Student s :list){
            System.out.println(s.getNIO()+""+s.getName()+""+s.getAge());
            System.out.println("***********************");
        }

//
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入学生姓名");
//        String s = scanner.nextLine();
//
//        Demo01.Student student = Function.find(list, s);
//        if (student!=null){
//            System.out.println(student);
//        }else
//            System.out.println("没有这个学生");

        double num =0;
        int avgnum = 0;
        for (int i = 0; i < list.size(); i++) {
            num+=(list.get(i).getAge());
            avgnum=(int)(num/list.size());
        }
        System.out.println("平均年龄"+avgnum);

        double max = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAge()>max){
                max=list.get(i).getAge();
            }
        }
        System.out.println("最大年龄"+max);

      double min =list.get(0).getAge();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAge()<min){
                min=list.get(i).getAge();
            }
        }
        System.out.println("最小年龄"+min);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals("桑启")){
                list.get(i).setName("关羽");
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAge() < 20) {
                list.remove(i);
                i--;
            }
        }
        for (Student student : list) {
            System.out.println(student);
        }

    }

}
